module("framework.helper.recthelper", package.seeall)

---@class recthelper
local recthelper = {}

---@type Framework.UGUI.RectTrHelper
local CSRectTrHelper = Framework.UGUI.RectTrHelper

---@param rectTransform UnityEngine.RectTransform
---@param height number
function recthelper.setHeight(rectTransform, height)
    CSRectTrHelper.SetHeight(rectTransform, height)
end

---@param rectTransform UnityEngine.RectTransform
---@return number
function recthelper.getHeight(rectTransform)
    return CSRectTrHelper.GetHeight(rectTransform)
end

---@param rectTransform UnityEngine.RectTransform
---@param width number
function recthelper.setWidth(rectTransform, width)
    CSRectTrHelper.SetWidth(rectTransform, width)
end

---@param rectTransform UnityEngine.RectTransform
---@return number
function recthelper.getWidth(rectTransform)
    return CSRectTrHelper.GetWidth(rectTransform)
end

---@param rectTransform UnityEngine.RectTransform
---@param width number
---@param height number
function recthelper.setSize(rectTransform, width, height)
    CSRectTrHelper.SetSize(rectTransform, width, height)
end

---@param rectTransform UnityEngine.RectTransform
---@return number
function recthelper.getAnchorX(rectTransform)
    return CSRectTrHelper.GetAnchorX(rectTransform)
end

---@param rectTransform UnityEngine.RectTransform
---@return number
function recthelper.getAnchorY(rectTransform)
    return CSRectTrHelper.GetAnchorY(rectTransform)
end

---@param rectTransform UnityEngine.RectTransform
---@return number, number
function recthelper.getAnchor(rectTransform)
    return CSRectTrHelper.GetAnchor(rectTransform, 0, 0)
end

---@param rectTransform UnityEngine.RectTransform
---@param anchorX number
function recthelper.setAnchorX(rectTransform, anchorX)
    CSRectTrHelper.SetAnchorX(rectTransform, anchorX)
end

---@param rectTransform UnityEngine.RectTransform
---@param anchorY number
function recthelper.setAnchorY(rectTransform, anchorY)
    CSRectTrHelper.SetAnchorY(rectTransform, anchorY)
end

---@param rectTransform UnityEngine.RectTransform
---@param anchorX number
---@param anchorY number
function recthelper.setAnchor(rectTransform, anchorX, anchorY)
    CSRectTrHelper.SetAnchor(rectTransform, anchorX, anchorY)
end

---屏幕坐标 转 UIAnchor坐标
---@param screenPos UnityEngine.Vector2
---@param rect UnityEngine.RectTransform
---@return UnityEngine.Vector2
function recthelper.screenPosToAnchorPos(screenPos, rect)
    local uiCamera = CameraMgr.instance:getUICamera()
    return CSRectTrHelper.ScreenPosToAnchorPos(screenPos, rect, uiCamera)
end

---屏幕坐标 转 UIAnchor坐标
---@param screenPos UnityEngine.Vector2
---@param rect UnityEngine.RectTransform
---@return number, number
function recthelper.screenPosToAnchorPos2(screenPos, rect)
    local uiCamera = CameraMgr.instance:getUICamera()
    return CSRectTrHelper.ScreenPosToAnchorPos2(screenPos, rect, uiCamera, nil, nil)
end

---UI世界坐标 转 相对UIAnchor坐标
---@param rectTrWorldPos UnityEngine.Vector3
---@param relativeRectTr UnityEngine.RectTransform
---@return UnityEngine.Vector2
function recthelper.rectToRelativeAnchorPos(rectTrWorldPos, relativeRectTr)
    local uiCamera = CameraMgr.instance:getUICamera()
    local screenPos = uiCamera:WorldToScreenPoint(rectTrWorldPos)
    return CSRectTrHelper.ScreenPosToAnchorPos(screenPos, relativeRectTr, uiCamera)
end

---UI世界坐标 转 相对UIAnchor坐标
---@param rectTrWorldPos UnityEngine.Vector3
---@param relativeRectTr UnityEngine.RectTransform
---@return number, number
function recthelper.rectToRelativeAnchorPos2(rectTrWorldPos, relativeRectTr)
    local uiCamera = CameraMgr.instance:getUICamera()
    local screenPos = uiCamera:WorldToScreenPoint(rectTrWorldPos)
    return CSRectTrHelper.ScreenPosToAnchorPos2(screenPos, relativeRectTr, uiCamera, nil, nil)
end

---3D坐标 转 UIAnchor坐标
---@param worldPos UnityEngine.Vector3
---@param planeRectTr UnityEngine.RectTransform
---@param uiCamera UnityEngine.Camera
---@param camera3d UnityEngine.Camera
---@return UnityEngine.Vector2
function recthelper.worldPosToAnchorPos(worldPos, planeRectTr, uiCamera, camera3d)
    uiCamera = uiCamera or CameraMgr.instance:getUICamera()
    camera3d = camera3d or CameraMgr.instance:getMainCamera()
    return CSRectTrHelper.WorldPosToAnchorPos(worldPos, planeRectTr, uiCamera, camera3d)
end

---3D坐标 转 UIAnchor坐标
---@param worldPos UnityEngine.Vector3 世界坐标
---@param planeRectTr UnityEngine.RectTransform  
---@param uiCamera UnityEngine.Camera       2D相机
---@param camera3d UnityEngine.Camera       3D相机
---@return number, number
function recthelper.worldPosToAnchorPos2(worldPos, planeRectTr, uiCamera, camera3d)
    uiCamera = uiCamera or CameraMgr.instance:getUICamera()
    camera3d = camera3d or CameraMgr.instance:getMainCamera()
    return CSRectTrHelper.WorldPosToAnchorPos2(worldPos, planeRectTr, uiCamera, camera3d, nil, nil)
end

---3D坐标 转 UIAnchor坐标
---@param worldPosX number 世界坐标X
---@param worldPosY number 世界坐标Y
---@param worldPosZ number 世界坐标Z
---@param planeRectTr UnityEngine.RectTransform 
---@param uiCamera UnityEngine.Camera   UI相机
---@param camera3d UnityEngine.Camera   3D相机
---@return number, number
function recthelper.worldPosToAnchorPosXYZ(worldPosX, worldPosY, worldPosZ, planeRectTr, uiCamera, camera3d)
    uiCamera = uiCamera or CameraMgr.instance:getUICamera()
    camera3d = camera3d or CameraMgr.instance:getMainCamera()
    return CSRectTrHelper.WorldPosToAnchorPosXYZ(worldPosX, worldPosY, worldPosZ, planeRectTr, uiCamera, camera3d, nil, nil)
end

---屏幕坐标转换为3D空间的世界坐标，必须有参照物体才知道转换到那个z轴平面的世界坐标。
---@param screenPos     UnityEngine.Vector2          屏幕坐标
---@param worldCamera   UnityEngine.Camera          主相机
---@param refGOWorldPos UnityEngine.Vector3         世界的一个参考物体的世界坐标，需要转换到与此参照物体同一个z轴坐标的
---@return UnityEngine.Vector3
function recthelper.screenPosToWorldPos(screenPos, worldCamera, refGOWorldPos)
    worldCamera = worldCamera or CameraMgr.instance:getMainCamera()
    return CSRectTrHelper.ScreenPosToWorldPos(screenPos, worldCamera, refGOWorldPos)
end

---屏幕坐标转换为3D空间的世界坐标，必须有参照物体才知道转换到那个z轴平面的世界坐标。
---@param screenPos     UnityEngine.Vector2         屏幕坐标
---@param worldCamera   UnityEngine.Camera          主相机
---@param refGOWorldPos UnityEngine.Vector3         世界的一个参考物体的世界坐标，需要转换到与此参照物体同一个z轴坐标的
---@return number, number, number
function recthelper.screenPosToWorldPos3(screenPos, worldCamera, refGOWorldPos)
    worldCamera = worldCamera or CameraMgr.instance:getMainCamera()
    return CSRectTrHelper.ScreenPosToWorldPos3(screenPos, worldCamera, refGOWorldPos, nil, nil, nil)
end

---UI坐标转屏幕坐标
---@param rectTransform UnityEngine.RectTransform
---@param canvas UnityEngine.Canvas
---@return UnityEngine.Vector2
function recthelper.uiPosToScreenPos(rectTransform, canvas)
    canvas = canvas or ViewMgr.instance:getUICanvas()
    return CSRectTrHelper.UIPosToScreenPos(rectTransform, canvas)
end

--UI坐标转屏幕坐标
---@param rectTransform UnityEngine.RectTransform
---@param canvas UnityEngine.Canvas
---@return number, number
function recthelper.uiPosToScreenPos2(rectTransform, canvas)
    canvas = canvas or ViewMgr.instance:getUICanvas()
    return CSRectTrHelper.UIPosToScreenPos2(rectTransform, canvas, nil, nil)
end

---@param rectTransform UnityEngine.RectTransform
---@param uiCamera UnityEngine.Camera
---@param screenPosX number
---@param screenPosY number
---@return boolean
function recthelper.screenPosInRect(rectTransform, uiCamera, screenPosX, screenPosY)
    uiCamera = uiCamera or CameraMgr.instance:getUICamera()
    return CSRectTrHelper.ScreenPosInRect(rectTransform, uiCamera, screenPosX, screenPosY, nil)
end

function recthelper.worldPosToScreenPoint(worldCamera, worldPosX, worldPosY, worldPosZ)
    worldCamera = worldCamera or CameraMgr.instance:getMainCamera()
    return CSRectTrHelper.WorldPosToScreenPoint(worldCamera, worldPosX, worldPosY, worldPosZ, nil, nil, nil)
end

return recthelper
